//引入nanoid
import {nanoid} from 'nanoid'
export default{
    namespaced:true, //配置对应的命名空间，标识模块化
    actions:{
        addOdd(context,name){
            //奇数添加
            if(context.state.personList.length %2){
                if(name === ' ') return
                context.commit('add',name)
            }else {
                alert('不满足条件，不予追加!')
            }
        },
        deleteWait(context){
            //删除头部元素，直接调用
            setTimeout(() => {
                context.commit('deleteFirst')
            }, 500);
        }
    },
    mutations:{
        //mutation参数就是state和需要操作的数据
        add(state,name){
            state.personList.unshift({id:nanoid(),name:name}) //头插元素
        },
        deleteFirst(state){
            state.personList.shift() //移除头部元素
        }
    },
    state:{
        personList:[
            {id:nanoid(),name:'zhangsan'},
            {id:nanoid(),name:'lisi'},
            {id:nanoid(),name:'wangwu'},
            {id:nanoid(),name:'zhaoliu'},
        ]
    }
}